package com.malphite.mongodemo.mongo;

import com.malphite.mongodemo.bean.User;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.bson.codecs.configuration.CodecRegistries;

import java.util.ArrayList;

//document映射为pojo
public class Mongo2Pojo {

    public static void main(String[] args) {

        PojoCodecProvider provider = PojoCodecProvider.builder().automatic(true).build();
        CodecRegistry defaultCodecRegistry = MongoClientSettings.getDefaultCodecRegistry();
        CodecRegistry codecRegistry = CodecRegistries.fromRegistries(defaultCodecRegistry, CodecRegistries.fromProviders(provider));
        MongoClient mongoClient = MongoClients.create("");
        MongoDatabase database = mongoClient.getDatabase("").withCodecRegistry(codecRegistry);
        MongoCollection<User> collection = database.getCollection("", User.class);
        ArrayList<User> arrayList = new ArrayList<>();
        collection.find().into(arrayList);

        collection.insertOne(new User());

    }

}
